<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/9/2
 * Time: 15:13
 */
namespace App\Services;

use App\Repositories\CreditCardHandleGrantRepository;
use App\Repositories\CreditCardHandleRepository;
use App\Repositories\UserLevelProfitRepository;

class CreditCardHandleService extends CommonService
{
    protected $creditCardHandleRepository;
    protected $creditCardHandleGrantRepository;
    protected $userLevelProfitRepository;

    public function __construct(CreditCardHandleRepository $creditCardHandleRepository,
                                CreditCardHandleGrantRepository $creditCardHandleGrantRepository,
                                UserLevelProfitRepository $userLevelProfitRepository)
    {
        $this->creditCardHandleRepository = $creditCardHandleRepository;
        $this->creditCardHandleGrantRepository = $creditCardHandleGrantRepository;
        $this->userLevelProfitRepository = $userLevelProfitRepository;
    }

    /**
     * 获取办卡列表
     * @param $page
     * @return \Illuminate\Http\JsonResponse
     */
    public function getHandleCardList($page, $userLevel, $invitedUserId)
    {
        $result = $this->creditCardHandleRepository->getList($page, 10);
        $data = [];
        if (!empty($result)) {
            foreach ($result as $key=>$value) {
                $data[$key] = $value;
                //服务商直推
                $indirectRelationsOne = $this->userLevelProfitRepository->byToProfitPercent(1, 6);
                $data[$key]['level_1_direct'] = roundNo($value['profit_money'] * $indirectRelationsOne->direct_profit_percent);
                //服务商间推
                $data[$key]['level_1_indirect'] = roundNo($value['profit_money'] * $indirectRelationsOne->indirect_profit_percent);
                //微客直推
                $indirectRelationsOne = $this->userLevelProfitRepository->byToProfitPercent(2, 6);
                $data[$key]['level_2_direct'] = roundNo($value['profit_money'] * $indirectRelationsOne->direct_profit_percent);
                //微客间推
                $data[$key]['level_2_indirect'] = roundNo($value['profit_money'] * $indirectRelationsOne->indirect_profit_percent);
                $data[$key]['user_level'] = $userLevel;
                $data[$key]['share_detail'] = $this->shareParams($invitedUserId, $value['id'],
                    $value['card_url'], $value['title'], $value['keywords']);
            }
        }
        return $this->responseJson(0, $data);
    }

    /**
     * 办卡进度查询列表
     * @param $page
     * @return \Illuminate\Http\JsonResponse
     */
    public function selectHandleResult($page)
    {
        $data = $this->creditCardHandleRepository->selectHandleResult($page, 9);
        return $this->responseJson(0, $data);
    }

    /**
     * 办理规则
     * @return \Illuminate\Http\JsonResponse
     */
    public function handleRules()
    {
        $result = $this->creditCardHandleRepository->handleRules();
        $data = [];
        if (!empty($result)) {
            foreach ($result as $key=>$value) {
                $data[$key] = $value;
                //服务商直推
                $indirectRelationsOne = $this->userLevelProfitRepository->byToProfitPercent(1, 6);
                $data[$key]['level_1_direct'] = roundNo($value['profit_money'] * $indirectRelationsOne->direct_profit_percent);
                //服务商间推
                $data[$key]['level_1_indirect'] = roundNo($value['profit_money'] * $indirectRelationsOne->indirect_profit_percent);
                //微客直推
                $indirectRelationsOne = $this->userLevelProfitRepository->byToProfitPercent(2, 6);
                $data[$key]['level_2_direct'] = roundNo($value['profit_money'] * $indirectRelationsOne->direct_profit_percent);
                //微客间推
                $data[$key]['level_2_indirect'] = roundNo($value['profit_money'] * $indirectRelationsOne->indirect_profit_percent);
            }
        }
        //获取办卡标准文案
        $wordPress = $this->creditCardHandleGrantRepository->getGrantAll();
        $res = [
            'handle_rules' => $data,
            'handle_grant' => $wordPress,
            'handle_remark' => '普通会员及超级会员无法享受分享办理成功佣金，需支付升级为微客方可享受。同时信用卡必须是通过本平台申请信用卡成功的，才可获得推广佣金。',
            'handle_tips_title' => '请务必将以下注意事项告知客户',
            'handle_tips_content_1' => '收到银行寄的信用卡后，一定要去银行激活卡片，以免影响客户在银行的诚信记录！',
            'handle_tips_content_2' => '信用卡申请被拒后，不要在同一家银行再次申请，以免多次查询征信，造成征信不良。',
        ];
        return $this->responseJson(0, $res);
    }
}